package com.hswealth;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.stream.events.EndDocument;
import javax.xml.stream.events.StartDocument;

public class MainMenu {
	
	static List<Bills> billsList = new ArrayList<>();
	
	static {
		billsList.add(new Bills("吃饭支出","支付宝","支出",247.0,"2021-03-21","家庭支出"));
		billsList.add(new Bills("工资收入","工资卡","收入",10000.0,"2021-04-16","发工资了"));
		billsList.add(new Bills("交通出行","支付宝","支出",50.0,"2021-04-19","打车回家"));
		billsList.add(new Bills("礼金支出","微信","支出",1000.0,"2021-05-01","朋友结婚"));
		billsList.add(new Bills("股票收入","支付宝","收入",200.0,"2021-05-10","股票分红"));
	}
	
	public static void main(String[] args) {
		run();
	}
	
	public static void showMain() {
		System.out.println("-----------------------随手记------------------------");
		System.out.println("1.添加账务    2.删除账务    3.查询账务    4.退出系统");
		System.out.println("请输入功能序号【1-4】：");
	}
	
	public static void run() {
		
		showMain();
		
		//创建一个flag
		boolean flag = true;
		//创建扫描器
		Scanner scanner = new Scanner(System.in);
		while(flag) {
			//获取输入的选项
			int op = scanner.nextInt();
			//判断1，2，3，4
			switch(op) {
			case 1:
//				System.out.println("添加账务");
				createBills();
				break;
			case 2:
//				System.out.println("删除账务");
				deleteBills();
				break;
			case 3:
//				System.out.println("查询账务");
				select();
				break;
			case 4:
				flag = false;
				break;
			default:
				System.out.println("请输入正确的功能序号：");
			}
			
		}
		System.out.println("感谢使用，再见");
			
		
	}

	private static void deleteBills() {
		System.out.println("请输入想要删除的账务ID：");
		Scanner scanner = new Scanner(System.in);
		int id = scanner.nextInt();
		billsList.remove(id-1);
		System.out.println("删除账务成功");
		
	}

	private static void createBills() {
		Scanner scanner = new Scanner(System.in);
		Bills bill = new Bills();
		
		System.out.println("请输入类别：");
		bill.setName(scanner.next());
		System.out.println("请输入账户：");
		bill.setAccount(scanner.next());
		System.out.println("请输入类型：");
		bill.setType(scanner.next());
		System.out.println("请输入金额：");
		bill.setTotal(scanner.nextDouble());
		System.out.println("请输入时间：");
		bill.setTime(scanner.next());
		System.out.println("请输入备注：");
		bill.setDesc(scanner.next());
		
		billsList.add(bill);
		System.out.println("添加账户成功");
	}

	/**
	 * 三种查询：1、查询所有；2、按照时间区间查询；3、收入和支出类型查询
	 */
	private static void select() {
		System.out.println("随手记>>账务查询");
		System.out.println("1.查询所有        2.按照时间区间查询        3.按照类型查询");
		Scanner scanner = new Scanner(System.in);
		int op = scanner.nextInt();
		switch (op) {
		case 1:
			selectAll();
			break;
		case 2:
			selectByDate();
			break;
		case 3:
			selectByType();
			break;
		default:
			System.out.println("请输入正确的序号：");
			break;
		}
		//展示上一级主菜单
		showMain();
		
	}

	private static void selectByType() {
		System.out.println("随手记>>账务查询>>按照类型查询");
		System.out.println("请输入：收入/支出");
		Scanner scanner = new Scanner(System.in);
		String type = scanner.next();
		
		List<Bills> collect = billsList.stream().filter(billsList -> {
			return billsList.getType().equals(type);
		}).collect(Collectors.toList());
		print(collect);
		
	}

	private static void selectByDate() {
		//创建一个时间格式化的对象
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		System.out.println("随手记>>账务查询>>按照时间区间查询");
		System.out.println("请输入开始时间：");
		Scanner scanner = new Scanner(System.in);
		String start = scanner.next();
		System.out.println("请输入结束时间：");
		String end = scanner.next();
		
		List<Bills> collect = billsList.stream().filter(billsList -> {
			String time = billsList.getTime();
			//把字符串解析成具体的时间
			try {
				Date startDate = format.parse(start);
				Date endDate = format.parse(end);
				Date timeDate = format.parse(time);
				if (timeDate.before(endDate) && timeDate.after(startDate)) {
					return true;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return false;
		}).collect(Collectors.toList());
		print(collect);
		
	}

	private static void selectAll() {
		System.out.println("随手记>>账务查询>>查询所有");
		print(billsList);
		
	}
	
	public static void print(List<Bills> billsList) {
		System.out.println("ID\t\t类别\t\t\t账户\t\t类型\t\t金额\t\t\t时间\t\t\t\t备注");
		for (int i = 0; i < billsList.size(); i++) {
			Bills bills = billsList.get(i);
			System.out.println(i+1 +"\t\t" + bills.getName() + "\t\t\t" + bills.getAccount() + "\t\t"
					+ bills.getType() + "\t\t" + bills.getTotal() + "\t\t\t" + bills.getTime() + "\t\t\t" + bills.getDesc());
		}
	}

}
